package com.ruoyi.depot.service.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.core.domain.ProductCates;
import com.ruoyi.common.core.domain.TreeSelect;
import com.ruoyi.common.core.domain.depot.PreWarehouse;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.spring.SpringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.depot.mapper.PreWarehouseMapper;
import com.ruoyi.depot.service.IPreWarehouseService;

/**
 * 仓库列表Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-11-01
 */
@Service
public class PreWarehouseServiceImpl implements IPreWarehouseService 
{
    @Autowired
    private PreWarehouseMapper preWarehouseMapper;

    /**
     * 查询仓库列表
     * 
     * @param warehouseId 仓库列表主键
     * @return 仓库列表
     */
    @Override
    public PreWarehouse selectPreWarehouseByWarehouseId(Long warehouseId)
    {
        return preWarehouseMapper.selectPreWarehouseByWarehouseId(warehouseId);
    }

    /**
     * 查询仓库列表列表
     * 
     * @param preWarehouse 仓库列表
     * @return 仓库列表
     */
    @Override
    @DataScope(PreWarehouseAlias = "pw")
    public List<PreWarehouse> selectPreWarehouseList(PreWarehouse preWarehouse)
    {
        return preWarehouseMapper.selectPreWarehouseList(preWarehouse);
    }

    /**
     * 新增仓库列表
     * 
     * @param preWarehouse 仓库列表
     * @return 结果
     */
    @Override
    public int insertPreWarehouse(PreWarehouse preWarehouse)
    {
        preWarehouse.setAddTime(DateUtils.getNowDate());

        // 查询最新的编号
        PreWarehouse prewarehouse = preWarehouseMapper.selectNumber();

        if(prewarehouse != null){
            // 自动生成编号递增
            int currentNumber = Integer.parseInt(prewarehouse.getWarehouseNumber());
            int incrementedNumber = currentNumber + 1;
            preWarehouse.setWarehouseNumber(String.valueOf(incrementedNumber));
        } else {
            preWarehouse.setWarehouseNumber(String.valueOf(1));
        }

        return preWarehouseMapper.insertPreWarehouse(preWarehouse);
    }

    /**
     * 修改仓库列表
     * 
     * @param preWarehouse 仓库列表
     * @return 结果
     */
    @Override
    public int updatePreWarehouse(PreWarehouse preWarehouse)
    {
        preWarehouse.setUpdateTime(DateUtils.getNowDate());
        return preWarehouseMapper.updatePreWarehouse(preWarehouse);
    }

    /**
     * 批量删除仓库列表
     * 
     * @param warehouseIds 需要删除的仓库列表主键
     * @return 结果
     */
    @Override
    public int deletePreWarehouseByWarehouseIds(Long[] warehouseIds)
    {
        return preWarehouseMapper.deletePreWarehouseByWarehouseIds(warehouseIds);
    }

    /**
     * 删除仓库列表信息
     * 
     * @param warehouseId 仓库列表主键
     * @return 结果
     */
    @Override
    public int deletePreWarehouseByWarehouseId(Long warehouseId)
    {
        return preWarehouseMapper.deletePreWarehouseByWarehouseId(warehouseId);
    }

    @Override
    public List<TreeSelect> selectPreWarehouseTreeList(PreWarehouse preWarehouse) {
        List<PreWarehouse> preWarehouseList = SpringUtils.getAopProxy(this).selectPreWarehouseList(preWarehouse);
        return buildPreWarehouseTreeSelect(preWarehouseList);
    }

    @Override
    public List<PreWarehouse> buildPreWarehouseTree(List<PreWarehouse> preWarehouseList) {
        List<PreWarehouse> returnList = new ArrayList<>();
        List<Long> collect = preWarehouseList.stream().map(PreWarehouse::getWarehouseId).collect(Collectors.toList());
        for (PreWarehouse preWarehouse : preWarehouseList){
            //如果是顶节点，遍历该父节点下的所以子节的
            if(!collect.contains(preWarehouse.getParentId())){
                recursionFn(preWarehouseList,preWarehouse);
                returnList.add(preWarehouse);
            }
        }
        if (returnList.isEmpty())
        {
            returnList = preWarehouseList;
        }
        return returnList;
    }

    @Override
    public List<TreeSelect> buildPreWarehouseTreeSelect(List<PreWarehouse> preWarehouseList) {
        List<PreWarehouse> preWarehouse = buildPreWarehouseTree(preWarehouseList);
        return preWarehouse.stream().map(TreeSelect::new).collect(Collectors.toList());
    }

    /**
     * 递归列表
     * @param preWarehouseList 子级
     * @param preWarehouse 父级
     */
    private void recursionFn(List<PreWarehouse> preWarehouseList , PreWarehouse preWarehouse){
        // 得到子节点列表
        List<PreWarehouse> childList = getChildList(preWarehouseList, preWarehouse);
        preWarehouse.setChildrenPreWarehouse(childList);
        for (PreWarehouse newPreWarehouse : childList)
        {
            if (hasChild(preWarehouseList, newPreWarehouse))
            {
                recursionFn(preWarehouseList, newPreWarehouse);
            }
        }
    }

    /**
     * 获得子节点
     * @param preWarehouseList
     * @param preWarehouse
     * @return
     */
    private List<PreWarehouse> getChildList(List<PreWarehouse> preWarehouseList, PreWarehouse preWarehouse){
        List<PreWarehouse> returnList = new ArrayList<>();
        Iterator<PreWarehouse> iterator = preWarehouseList.iterator();
        while (iterator.hasNext()){
            PreWarehouse n = (PreWarehouse) iterator.next();
            if (StringUtils.isNotNull(n.getParentId()) && n.getParentId().longValue() == preWarehouse.getWarehouseId().longValue())
            {
                returnList.add(n);
            }
        }
        return returnList;
    }

    /**
     * 判断是否有子节点
     */
    private boolean hasChild(List<PreWarehouse> preWarehouseList, PreWarehouse preWarehouse)
    {
        return getChildList(preWarehouseList, preWarehouse).size() > 0;
    }
}
